"""PostGIS空间数据模型

定义用于教学演示的空间数据实体，包括点要素、面要素等。
"""

from sqlalchemy import Column, Integer, String, DateTime, Text
from sqlalchemy.ext.declarative import declarative_base
from geoalchemy2 import Geometry
from datetime import datetime

Base = declarative_base()


class SpatialPoint(Base):
    """空间点要素模型
    
    用于演示点在面内的空间关系
    """
    __tablename__ = 'spatial_points'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), nullable=False, comment='点要素名称')
    description = Column(Text, comment='点要素描述')
    geometry = Column(Geometry('POINT', srid=4326), nullable=False, comment='点几何')
    category = Column(String(50), comment='点要素类别')
    created_at = Column(DateTime, default=datetime.utcnow, comment='创建时间')
    
    def __repr__(self):
        return f"<SpatialPoint(id={self.id}, name='{self.name}')>"


class SpatialPolygon(Base):
    """空间面要素模型
    
    用于演示面与面之间的空间关系（包含、相交等）
    """
    __tablename__ = 'spatial_polygons'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), nullable=False, comment='面要素名称')
    description = Column(Text, comment='面要素描述')
    geometry = Column(Geometry('POLYGON', srid=4326), nullable=False, comment='面几何')
    category = Column(String(50), comment='面要素类别')
    area = Column(Integer, comment='面积（平方米）')
    created_at = Column(DateTime, default=datetime.utcnow, comment='创建时间')
    
    def __repr__(self):
        return f"<SpatialPolygon(id={self.id}, name='{self.name}')>"


class SpatialRegion(Base):
    """空间区域模型
    
    用于演示大区域与小区域的包含关系
    """
    __tablename__ = 'spatial_regions'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), nullable=False, comment='区域名称')
    description = Column(Text, comment='区域描述')
    geometry = Column(Geometry('POLYGON', srid=4326), nullable=False, comment='区域几何')
    region_type = Column(String(50), comment='区域类型（省、市、区等）')
    parent_id = Column(Integer, comment='父级区域ID')
    level = Column(Integer, comment='行政级别')
    created_at = Column(DateTime, default=datetime.utcnow, comment='创建时间')
    
    def __repr__(self):
        return f"<SpatialRegion(id={self.id}, name='{self.name}', type='{self.region_type}')>"